package com.bruce.starts.demo.commonerror.controller;

import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author luotuan
 * @Description
 * @create 2021-05-11 17:02
 **/
@RestController
@RequestMapping("logging")
@Log4j2
public class LoggingController {
    //private static final Logger log = LoggerFactory.getLogger(LoggingController.class);
    @GetMapping
    public void index() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("debug1");
        log.debug("debug1:" + slowString("debug1"));
        stopWatch.stop();
        stopWatch.start("debug2");
        log.debug("debug2:{}", slowString("debug2"));
        stopWatch.stop();
        stopWatch.start("debug3");
        if (log.isDebugEnabled()) {
            log.debug("debug3:{}", slowString("debug3"));
        }
        stopWatch.stop();
        stopWatch.start("debug4");
        log.debug("debug4:{}",()->slowString("debug4"));
        stopWatch.stop();
        log.info(stopWatch.prettyPrint());
    }

    private String slowString(String s) {
        System.out.println("slowString called via " + s);
        try {
            TimeUnit.SECONDS.sleep(1);
        }
        catch (InterruptedException e) {

        }
        return "ok";
    }
}
